package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.HomeGoodsDao;
import com.sun.showcase.biz.mall.service.GoodsService;
import com.sun.showcase.biz.mall.service.HomeGoodsService;
import com.sun.showcase.client.domain.mall.HomeGoods;
import com.sun.showcase.client.query.mall.GoodsQuery;
import com.sun.showcase.client.query.mall.HomeGoodsQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("homeGoodsService")
@Transactional
public class HomeGoodsServiceImpl implements HomeGoodsService{
	@Autowired
	private HomeGoodsDao homeGoodsDao;
	@Autowired
	private GoodsService goodsService;
	
	public void setHomeGoodsDao(HomeGoodsDao dao) {
		this.homeGoodsDao = dao;
	}

	public DataGrid datagrid(HomeGoodsQuery homeGoodsQuery) {
		DataGrid j = new DataGrid();
		Pager<HomeGoods> pager  = find(homeGoodsQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<HomeGoodsQuery> getQuerysFromEntitys(List<HomeGoods> homeGoodss) {
		List<HomeGoodsQuery> homeGoodsQuerys = new ArrayList<HomeGoodsQuery>();
		if (homeGoodss != null && homeGoodss.size() > 0) {
			for (HomeGoods tb : homeGoodss) {
				HomeGoodsQuery b = new HomeGoodsQuery();
				BeanUtils.copyProperties(tb, b);
				homeGoodsQuerys.add(b);
			}
		}
		return homeGoodsQuerys;
	}

	private Pager<HomeGoods> find(HomeGoodsQuery homeGoodsQuery) {
		Page<HomeGoods> page = PageHelper.startPage(homeGoodsQuery.getPage().intValue(), homeGoodsQuery.getRows().intValue());
		List<HomeGoods> list = homeGoodsDao.findList(homeGoodsQuery);
		Pager<HomeGoods> pager = new Pager<HomeGoods>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public HomeGoods add(HomeGoodsQuery homeGoodsQuery) {
		HomeGoods t = new HomeGoods();
		BeanUtils.copyProperties(homeGoodsQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		homeGoodsDao.save(t);
		homeGoodsQuery.setId(t.getId());
		return t;
		
	}

	public void update(HomeGoodsQuery homeGoodsQuery) {
		HomeGoods t = homeGoodsDao.getById(homeGoodsQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(homeGoodsQuery, t);
	    	homeGoodsDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(HomeGoodsQuery homeGoodsQuery) {
	    homeGoodsDao.updatePart(homeGoodsQuery);
	}

	public void delete(String[] ids) {
		homeGoodsDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		homeGoodsDao.deletePt(ids);
	}

	public HomeGoods get(HomeGoodsQuery homeGoodsQuery) {
		return homeGoodsDao.getById(homeGoodsQuery.getId());
	}

	public HomeGoods get(String id) {
		return homeGoodsDao.getById(id);
	}

	
	public List<HomeGoodsQuery> listAll(HomeGoodsQuery homeGoodsQuery) {
	    List<HomeGoods> list = homeGoodsDao.findList(homeGoodsQuery);
		List<HomeGoodsQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public DataGrid newList(HomeGoodsQuery homeGoodsQuery) {
		/*HomeGoodsQuery homeGoodsQuery = new HomeGoodsQuery();
		homeGoodsQuery.setStatus(StringConstant.ONE);
		homeGoodsQuery.setOrderColum("order_num");
		homeGoodsQuery.setType(StringConstant.ZERO);
		homeGoodsQuery.setPage(1L);
		homeGoodsQuery.setRows(10L);
		DataGrid dg = this.datagrid(homeGoodsQuery);
		List<Goods> list = new ArrayList<Goods>();
		for(HomeGoodsQuery hg : (List<HomeGoodsQuery>)dg.getRows()) {
			list.add(goodsService.get(hg.getGoodsId()));
		}
		return list;*/
		GoodsQuery gq = new GoodsQuery();
		gq.setHomeType(StringConstant.ZERO);
		gq.setActiveFlag(StringConstant.ONE);
		gq.setPublishStatus(StringConstant.ONE);
		gq.setPage(homeGoodsQuery.getPage());
		gq.setRows(homeGoodsQuery.getRows());
		return goodsService.homeDatagrid(gq);
	}

	@Override
	public DataGrid hotList(HomeGoodsQuery homeGoodsQuery) {
		/*HomeGoodsQuery homeGoodsQuery = new HomeGoodsQuery();
		homeGoodsQuery.setStatus(StringConstant.ONE);
		homeGoodsQuery.setOrderColum("order_num");
		homeGoodsQuery.setType(StringConstant.ONE);
		homeGoodsQuery.setPage(1L);
		homeGoodsQuery.setRows(10L);
		DataGrid dg = this.datagrid(homeGoodsQuery);
		List<Goods> list = new ArrayList<Goods>();
		for(HomeGoodsQuery hg : (List<HomeGoodsQuery>)dg.getRows()) {
			list.add(goodsService.get(hg.getGoodsId()));
		}*/
		GoodsQuery gq = new GoodsQuery();
		gq.setHomeType(StringConstant.ONE);
		gq.setActiveFlag(StringConstant.ONE);
		gq.setPublishStatus(StringConstant.ONE);
		gq.setPage(homeGoodsQuery.getPage());
		gq.setRows(homeGoodsQuery.getRows());
		return goodsService.homeDatagrid(gq);
	}
	
	
}
